﻿#include "dialog.h"
#include <QFileDialog>
#include "ui_dialog.h"
#include "qtOpenCV/convert.h"
#include "line2Dup.h"

Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::Dialog)
{
    ui->setupUi(this);

    box1 = new PictureBox(PictureBox::AUTO_ZOOM, ui->groupBoxTestImage);
    QVBoxLayout *vbox = new QVBoxLayout;
    vbox->addWidget(box1);
    ui->groupBoxTestImage->setLayout(vbox);


    box2 = new PictureBox(PictureBox::AUTO_ZOOM, ui->groupBoxTemplateImage);
    QVBoxLayout *vbox2 = new QVBoxLayout;
    vbox2->addWidget(box2);
    ui->groupBoxTemplateImage->setLayout(vbox2);

   // Qly::test02();
}

Dialog::~Dialog()
{
    delete ui;
}


void Dialog::on_pushButtonOpenTemplate_clicked()
{
    QString fileName = QFileDialog::getOpenFileName(this,
                                                    tr("Open Image"), "/home/jana", tr("Image Files (*.png *.jpg *.bmp)"));

 //   templateImg = cv::imread(fileName.toStdString());
 //   m_templateImage = cvMat2QImage(templateImg);//
    m_templateImage.load(fileName);
    templateImg = QImage2cvMat(m_templateImage);
    if(templateImg.channels() == 4)
        cv::cvtColor(templateImg, templateImg, cv::COLOR_BGRA2BGR);
    box2->setImage(m_templateImage);
}

void Dialog::on_pushButtonTrainModel_clicked()
{
    float angleStart = 0, angleEnd = 360, angleStep = 1;
    angleStart = ui->lineEditAngleMin->text().toDouble();
    angleEnd = ui->lineEditAngleMax->text().toDouble();
    angleStep = ui->lineEditAngleStep->text().toDouble();

    qDebug() << "angleStart, angleEnd, angleStep = " << angleStart << ", " << angleEnd << ", " << angleStep;
    num_feature = ui->lineEditFeatureNum->text().toInt();

    std::vector<float> angles;
    //angles.push_back(0);
    for(float i = angleStart; i <= angleEnd; i += angleStep)
    {
        angles.push_back(i);
    }
    sbMatch.trainModel(templateImg, angles, num_feature);
    m_templateImage = cvMat2QImage(sbMatch.modelImage());
    box2->setImage(m_templateImage);
    sbMatch.saveModel("./", "model");
}



void Dialog::on_pushButtonOpenImage_clicked()
{
    QString fileName = QFileDialog::getOpenFileName(this,
                                                    tr("Open Image"), "/home/jana", tr("Image Files (*.png *.jpg *.bmp)"));

    m_testImage.load(fileName);
    testImg = QImage2cvMat(m_testImage);
    if(testImg.channels() == 4)
        cv::cvtColor(testImg, testImg, cv::COLOR_BGRA2BGR);
    box1->setImage(m_testImage);
}



void drawRotatedRect(cv::Mat &image, const cv::RotatedRect &rr, cv::Scalar color, int thickness = 1)
{
    cv::Point2f pts[4];
    rr.points(pts);
    cv::line(image, pts[0], pts[1], color, thickness);
    cv::line(image, pts[1], pts[2], color, thickness);
    cv::line(image, pts[2], pts[3], color, thickness);
    cv::line(image, pts[0], pts[3], color, thickness);
}

void Dialog::on_pushButtonRunMatch_clicked()
{
    num_feature = ui->lineEditFeatureNum->text().toInt();
    float score = ui->lineEditMinScore->text().toDouble();
   // qDebug() << "score = " << score;
    int maxCount = 1;
    if(ui->radioButtonMulti->isChecked() )
    {
        maxCount = 100;
    }
    std::vector<Qly::RotatedShapeBasedMatching::MatchResult> mr = sbMatch.runMatch(testImg, score * 100, maxCount);
    qDebug() << "match = " << mr.size();
    for(size_t i = 0; i < mr.size(); i++)
    {
        qDebug() << "sim = " << mr[i].score << ", angle = " << mr[i].rect.angle;
       // drawRotatedRect(testImg, mr[i].rect, {0, 0, 255}, 2);
        sbMatch.drawFeatures(testImg, mr[i], 2, {0, 0, 255}, 2);
    }

    m_testImage = cvMat2QImage(testImg);
    box1->setImage(m_testImage);
}

void Dialog::on_pushButtonShowFeaturePts_clicked()
{
    //cv::imshow("feature points", featureImg);
    qDebug() <<  "featureImg.channels() = " << featureImg.channels();
    m_featureImage = cvMat2QImage(featureImg);
    qDebug() << "m_featureImage.format() = " << m_featureImage.format();
    box2->setImage(m_featureImage);
}
